#define _CRT_SECURE_NO_WARNINGS 1


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        vector<pair<TreeNode*, unsigned int>> q;
        q.push_back({ root, 1 });

        unsigned int ans = 0;
        while (!q.empty())
        {
            // unsigned int y1 = q[0].second;
            // unsigned int y2 = q.back().second;

            auto& [x1, y1] = q[0];
            auto& [x2, y2] = q.back();
            ans = max(ans, y2 - y1 + 1);

            vector<pair<TreeNode*, unsigned int>> temp;
            for (auto kv : q)
            {
                if (kv.first->left) temp.push_back({ kv.first->left, 2 * kv.second });
                if (kv.first->right) temp.push_back({ kv.first->right, 2 * kv.second + 1 });
            }
            q = temp;
        }
        return ans;
    }
};